package com.example.myweather.listener;

import com.example.myweather.R;
import com.example.myweather.database.DataBaseMapper;
import com.example.myweather.database.WidgetInitData;
import com.example.myweather.modelo.Weather;
import com.example.myweather.modelo.WeatherWebService;
import com.example.myweather.modelo.WebServicesXDotNet;
import com.example.myweather.widget.WeatherWidgetProvider;
import com.example.myweather.widget.WidgetConfigurationActivity;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RemoteViews;
import android.widget.Toast;

public class OKButtonConfigurationWidgetActivity implements OnClickListener{
	
	private Activity parent;
	
	public OKButtonConfigurationWidgetActivity(Activity parent){
		this.parent = parent;
	}
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		
		WidgetConfigurationActivity widgetConfigurationActivity= (WidgetConfigurationActivity) this.parent;
		
		int widgetId = widgetConfigurationActivity.getWidgetId();
		
		Context context = widgetConfigurationActivity.getApplicationContext();
		
		WidgetInitData widgetInitData = new WidgetInitData();
		
		widgetInitData.setWidgetId(Integer.toString(widgetId));
		
		widgetInitData.setCountryName(widgetConfigurationActivity.getInputCountryName());
		
		widgetInitData.setCityName(widgetConfigurationActivity.getInputCityName());
		
		DataBaseMapper dataBaseMapper = DataBaseMapper.getInstance();
		
		dataBaseMapper.addWidgetInitData(widgetInitData, context);
		
		WeatherWidgetProvider.updateWidget(widgetInitData, context);
		

		Intent resultValue = new Intent();
		resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
		this.parent.setResult(widgetConfigurationActivity.RESULT_OK, resultValue);
		this.parent.finish();
		
	}

}
